<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\library\Id as L_ID;
use think\cache\driver\Redis;

class FixTitle extends Command
{

    protected function configure()
    {
        $this->setName('FixTitle')
            ->setDescription('修复附件分类串名');
    }

    protected function execute(Input $input, Output $output)
    {
       $subject_class_list= db("subject_class")->column('*', 'id');
    // exit(print_r($subject_class_list));
        $a_list = db("subject_attachment")->field("id,subject_class_id")->select();
        if ($a_list) {
            // exit(print_r($a_list));
            foreach ($a_list as $a) {
                $subject_class_ids="";
                $subject_class_titles="";
                $subject_class_unique_ids="";
                if($a["subject_class_id"]>0){
                    if(isset($subject_class_list[$a["subject_class_id"]])){
                        $subject_class_ids=",".$subject_class_list[$a["subject_class_id"]]["id"].",";
                        $subject_class_titles=",".$subject_class_list[$a["subject_class_id"]]["title"].",";
                        $subject_class_unique_ids=",".$subject_class_list[$a["subject_class_id"]]["unique_id"].",";
                    }
                }
                db("subject_attachment")->where("id",$a["id"])->update([
                    "subject_class_ids"=>$subject_class_ids,
                    "subject_class_titles"=>$subject_class_titles,
                    "subject_class_unique_ids"=>$subject_class_unique_ids,
                ]);
            }
        }
        echo "finished" . PHP_EOL;
    }
}
